from flask import render_template, session, current_app, g, abort, jsonify, request

from info import constants, db
from info.models import User, News, Comment, CommentLike
from info.modules.news import news_blu
from info.utils.common import user_login
from info.utils.response_code import RET


@news_blu.route('/comment_like', methods=['POST'])
@user_login
def set_comment_like():
    # 点赞功能实现
    user = g.user
    if not user:
        return jsonify(errno=RET.SERVERERR, errmsg='用户未登陆')
    comment_id = request.json.get('comment_id')
    action = request.json.get('action')

    if not all([comment_id, action]):
        return jsonify(erron=RET.PARAMERR, errmsg='参数不完整')

    if action not in['add', 'remove']:
        return jsonify(erron=RET.PARAMERR, errmsg='参数错误')

    try:
        comment_id = int(comment_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='参数错误')

    try:
        comment_obj = Comment.query.get(comment_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库查询错误')

    if not comment_obj:
        return jsonify(errno=RET.NODATA, errmsg='该评论不存在')

    comment_like_obj = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first()
    if action == 'add':
        if not comment_like_obj:
            comment_like = CommentLike()
            comment_like.comment_id = comment_id
            comment_like.user_id = user.id
            db.session.add(comment_like)
            comment_obj.like_count += 1

    else:
        if comment_like_obj:
            db.session.delete(comment_like_obj)
            comment_obj.like_count -= 1

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库保存失败')

    return jsonify(errno=RET.OK, errmsg='OK')


@news_blu.route("/news_comment", methods=["POST"])
@user_login
def news_comment():
    """
    新闻评论功能
    1、接收参数 用户  新闻  评论内容  parant_id
    2、校验参数
    3、业务逻辑  往数据库中添加一条评论
    4、返回响应 返回响应的评论
    :return:
    """
    user = g.user
    if not user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")

    news_id = request.json.get("news_id")
    comment_str = request.json.get("comment")
    parent_id = request.json.get("parent_id")

    if not all([news_id, comment_str]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    try:
        news_id = int(news_id)
        if parent_id:
            parent_id = int(parent_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数类型错误")

    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询错误")

    if not news:
        return jsonify(errno=RET.NODATA, errmsg="该条新闻不存在")

    # 添加新闻评论，往评论表中添加数据
    comment = Comment()
    comment.news_id = news_id
    comment.user_id = user.id
    comment.content = comment_str
    if parent_id:
        comment.parent_id = parent_id

    try:
        db.session.add(comment)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库保存失败")
    # 查询出该条新闻下的评论条数
    comment_count = Comment.query.filter(Comment.news_id == news_id).count()

    return jsonify(errno=RET.OK, errmsg="ok", data=comment.to_dict(), comment_count=comment_count)


@news_blu.route("/news_collect", methods=["POST"])
@user_login
def news_collect():
    """
    新闻收藏和取消收藏功能
    1、接收参数
    2、校验参数
    3、收藏新闻和取消收藏
    4、返回响应
    :return:
    """
    user = g.user

    if not user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")

    news_id = request.json.get("news_id")
    action = request.json.get("action")

    if not all([news_id, action]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    if action not in ['collect', 'cancel_collect']:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    try:
        news_id = int(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="数据格式不正确")

    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询错误")

    if not news:
        return jsonify(errno=RET.NODATA, errmsg="该条新闻不存在")

    # 执行具体业务逻辑了
    if action == "collect":
        # 当我们去收藏一条新闻的时候，我们应该先去判断该用户书否收藏过该条新闻，如果没有收藏过，才去添加
        if news not in user.collection_news:
            user.collection_news.append(news)
    else:
        # 当我们去取消收藏的时候，如果该新闻在你的收藏列表中，才去删除
        if news in user.collection_news:
            user.collection_news.remove(news)

    return jsonify(errno=RET.OK, errmsg="OK")


@news_blu.route("/<int:news_id>")
@user_login
def detail(news_id):
    """
        详情页面渲染
        :param news_id:
        :return:
        """
    user = g.user

    # 1、查询点击排行新闻
    clicks_news = []
    try:
        clicks_news = News.query.order_by(News.clicks.desc()).limit(
            constants.CLICK_RANK_MAX_NEWS).all()  # [obj, obj, obj]
    except Exception as e:
        current_app.logger.error(e)

    # [obj, obj, obj] --> [{}, {}, {}, {}]
    clicks_news_li = [news.to_basic_dict() for news in clicks_news]

    news = None
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)

    if not news:
        abort(404)

    news.clicks += 1  # 104
    is_collected = False
    # 详情页收藏和已收藏是由is_collected
    # 在什么样的一个情况下  is_collected = True
    # 需求：如果 该用户收藏了该条新闻 is_collected = True
    # 1、保证用户存在
    # 2、新闻肯定存在
    # 3、该条新闻在用户收藏新闻的列表中
    # 4、用户收藏新闻的列表----> user.collection_news.all()  [news, news]
    if user and news in user.collection_news.all():
        is_collected = True

        # 四、查询出当前新闻下的所有的评论， 按照时间降序
        comments = []
        try:
            comments = Comment.query.filter(Comment.news_id == news_id).order_by(Comment.create_time.desc()).all()
        except Exception as e:
            current_app.logger.error(e)

        comment_like_ids = []
        if user:
            comment_ids = [comment.id for comment in comments]
            comment_likes = CommentLike.query.filter(CommentLike.user_id == user.id,
                                                     CommentLike.comment_id.in_(comment_ids)).all()
            comment_like_ids = [comment_like.comment_id for comment_like in comment_likes]

        # comments_dict_li = [comment.to_dict() for comment in comments]
        comments_dict_li = []
        for comment in comments:
            comment_dict = comment.to_dict()
            comment_dict["is_like"] = False
            # 如果comment_dict["is_like"] = True代表已经点赞
            # 如果该条评论的id在我的点赞评论id列表中
            if comment.id in comment_like_ids:
                comment_dict["is_like"] = True
            comments_dict_li.append(comment_dict)
        data = {
            "user_info": user.to_dict() if user else None,
            "clicks_news_li": clicks_news_li,
            "news": news.to_dict(),
            "is_collected": is_collected,
            "comments_dict_li": comments_dict_li
        }
        return render_template("news/detail.html",data=data)